package com.sp2p.action.front;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.shove.Convert;
import com.shove.data.DataException;
import com.shove.web.action.BasePageAction;
import com.sp2p.constants.IConstants;
import com.sp2p.service.NewsAndMediaReportService;

/**
 * 前台媒体报道
 * 
 * @author Administrator
 * 
 */
public class FrontMediaReportdAction extends BasePageAction {
	private static final long serialVersionUID = 1L;
	public static Log log = LogFactory.getLog(FrontMediaReportdAction.class);
	private NewsAndMediaReportService mediaReportService;

	public NewsAndMediaReportService getMediaReportService() {
		return mediaReportService;
	}

	public void setMediaReportService(
			NewsAndMediaReportService mediaReportService) {
		this.mediaReportService = mediaReportService;
	}

	/**
	 * 初始化下载数据
	 * 
	 * @return
	 * @throws SQLException
	 * @throws DataException
	 */
	public String frontQueryMediaReportdInit() throws SQLException,
			DataException {
		return SUCCESS;
	}

	/**
	 * 查询下载资料列表
	 * 
	 * @return String
	 * @throws Exception
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("unchecked")
	public String frontQueryMediaReportdList() throws Exception {
		pageBean.setPageNum(request().getParameter("PAGE_NO_KEY"));
		pageBean.setPageSize(IConstants.PAGE_SIZE_5);
		mediaReportService.queryMediaReportPage(pageBean);
		List<Map<String, Object>> lists = pageBean.getPage();
		
		List<Map<String,Object>> listHistory = mediaReportService.queryHistoryMediaReport();
		// 截取内容字段 houli
		String remark = "";
		String formartTime = "";
		if (lists != null) {
			for (Map<String, Object> map : lists) {
				/*if (map.get("remark").toString().length() > 50) {
					remark = map.get("remark").toString().substring(0, 50);
					map.put("remark", remark + "...");
				}*/
				if (null != map.get("publishTime")) {
					formartTime = map.get("publishTime").toString().substring(0,10).replace("-", ".");
					map.put("publishTime", formartTime);
				}
			}
		}
		String remark_history = "";
		if (listHistory != null) {
			for (Map<String, Object> map : listHistory) {
				if (map.get("remark").toString().length() > 20) {
					remark_history = map.get("remark").toString().substring(0, 20);
					map.put("remark", remark_history + "...");
				}
				if (null != map.get("publishTime")) {
					formartTime = map.get("publishTime").toString().substring(0,10).replace("-", ".");
					map.put("publishTime", formartTime);
				}
			}
		}
		request().setAttribute("reportList", lists);
		request().setAttribute("currentPage", pageBean.getPageNum());
		request().setAttribute("totalPageNum", pageBean.getTotalPageNum());
		request().setAttribute("listHistory", listHistory);
		request().setAttribute("typeId", 32);
		/*this.setRequestToParamMap();*/
		return SUCCESS;
	}

	/**
	 * 根据Id获取下载资料详情
	 * 
	 * @return
	 * @throws Exception
	 */
	public String frontQueryMediaReportById() throws Exception {
		Long id = Convert.strToLong(request("id"), -1);
		try {
			Map<String, String> map = mediaReportService.getMediaReportById(id);
			if (null != map && map.size() > 0) {
				if (!StringUtils.isEmpty(map.get("publishTime"))) {
					String formatTime = map.get("publishTime").substring(0, 10)
							.replace("-", ".");
					map.put("publishTime", formatTime);
				}
			}
			request().setAttribute("map", map);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 根据Id获取媒体报道
	 * 
	 * @return
	 * @throws Exception
	 */
	public String frontMedialinkId() throws Exception {
		Long id = Convert.strToLong(request("id"), -1);
		try {
			paramMap = mediaReportService.getMediaReportById(id);
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}

		return SUCCESS;
	}

}
